import 'package:flutter/material.dart';
import 'pages/login.dart';
import 'routers/router.dart';
import 'package:provider/provider.dart';
import 'provider/user_info.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'widget/unknown_route.dart';
import 'utils/jpush.dart';
import 'pages/tabs.dart';
import 'services/user.dart';
import 'widget/my_loading.dart';
import 'package:flutter/services.dart';
import 'utils/device.dart';
import 'provider/cart.dart';
import 'pages/pay/pay_error.dart';
import 'pages/comment_list.dart';
import 'pages/order_detail.dart';
import 'pages/my_account.dart';
import 'pages/my_account/score_detail.dart';
import 'pages/my_account/check_in.dart';
import 'pages/collect.dart';
import 'pages/h5.dart';
void main() {
  runApp(MyApp());
  if(Device.isAndroid){
    SystemUiOverlayStyle style = SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        ///这是设置状态栏的图标和字体的颜色
        ///Brightness.light  一般都是显示为白色
        ///Brightness.dark 一般都是显示为黑色
        statusBarIconBrightness: Brightness.light
    );
    SystemChrome.setSystemUIOverlayStyle(style);
  }
}

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);
  // MyApp({Key key}) : super(key: key);

  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool isLogin;//是否有token
  bool isloaded=false;
  //初始化是否登录
  Future initLogin() async {
    bool getIsLogin=await User.hasLogin();//获取是不是登录
    setState(() {
      isLogin=getIsLogin;
      isloaded=true;//加载成功
    });
  }

  @override
  initState() {
    super.initState();
    initLogin();
    JPushTool jpushTool = JPushTool();
    jpushTool.init();
  }
  final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
  @override
  Widget build(BuildContext context) {
    if(isloaded){
      return MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (_) => Userinfo()),
          ChangeNotifierProvider(create: (_) => Cart()),
        ],
        child: MaterialApp(
          debugShowCheckedModeBanner:false,
          navigatorKey: navigatorKey,
          onUnknownRoute: (RouteSettings settings) {
            return MaterialPageRoute(builder: (context) {
              return UnknownRoute();
            });
          },
          onGenerateRoute: (RouteSettings settings) {
            // 统一处理
            final String name = settings.name;
            final Function pageContentBuilder = routes[name];
            if (pageContentBuilder != null) {
              if (settings.arguments != null) {
                final Route route = MaterialPageRoute(
                    builder: (context) =>
                        pageContentBuilder(context, arguments: settings.arguments));
                return route;
              }else{
                final Route route = MaterialPageRoute(
                    builder: (context) =>
                        pageContentBuilder(context));
                return route;
              }
            }
          },
          home: isLogin ? Tabs() : LoginPage(),
          localizationsDelegates: [
            GlobalCupertinoLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate
          ],
          supportedLocales: [
            const Locale('zh', 'CN'),
            const Locale('en', 'US'),
          ],
          theme: ThemeData(
              appBarTheme: AppBarTheme(elevation: 0),
              scaffoldBackgroundColor: Colors.grey[100],
              primaryColor: Colors.white,),
        ),
      );
    }else{
      return MaterialApp(
        home: Scaffold(
          body: MyLoadding(),
        ),
      );
    }

  }
}
